package com.syncy.process.states;

import com.syncy.process.ProcessStateAction;
import com.syncy.process.ProcessStatus;
import com.syncy.process.exceptions.WrongStateException;
import com.syncy.process.Process;

public class ProcessRunningState extends ProcessState {

	private Process process = null;
	
	public ProcessRunningState(Process process) {
		this.process = process;
		
		this.init();
	}
	
	public void init() {
		this.process.getRunningThread().start();
	}
	
	
	@Override
	public ProcessStateAction[] getValidActions() {
		return new ProcessStateAction[] { ProcessStateAction.STOP, ProcessStateAction.HALT };
	}

	@Override
	public ProcessStatus doTransition(ProcessStateAction action) {
		
		if(action == ProcessStateAction.STOP) {
			this.process.stop();
			
			return ProcessStatus.STOPPED; 
		} else if (action == ProcessStateAction.HALT) {
			Thread t = this.process.getRunningThread();
			t.interrupt();
			return ProcessStatus.STOPPED; 			
		} else {		
			throw new WrongStateException(action);
		} 
	}

}
